<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      :before-open="beforeOpen"
      :before-close="beforeClose"
      :upload-before="uploadBefore"
      :upload-after="uploadAfter"
      @row-del="rowDel"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      :page.sync="page"
      @on-load="onLoad"
    >
    </avue-crud>
  </basic-container>
</template>

<script>
import { getTopicList, getTopicDetail, removeTopic } from '@/api/topic/index.js'
import { baseUrl } from '@/api/baseUrl'
export default {
  data() {
    return {
      data: [],
      form: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      option: {
        tip: false,
        simplePage: true,
        searchShow: true,
        searchMenuSpan: 18,
        searchMenuPosition: 'right',
        tree: true,
        border: true,
        addBtn: false,
        index: true,
        // selection: true,
        editBtn: false,
        delBtn: false,
        viewBtn: true,
        menuWidth: 300,
        dialogClickModal: false,
        height: 700,
        addBtn: false,
        delBtn: true,
        labelWidth: 120,
        column: [
          {
            label: '标题',
            prop: 'title',
            maxlength: 20,
            searchLabelWidth: 60,
            showWordLimit: true,
            search: true,
            width: 150,
            rules: [
              {
                required: true,
                message: '请输入标题',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '内容',
            prop: 'content',
            span: 24,
            type: 'textarea',
            maxlength: 100,
            showWordLimit: true,
            rules: [
              {
                required: true,
                message: '请输入内容',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '话题图片',
            prop: 'mediaUrl',
            dataType: 'string',
            listType: 'picture-img',
            type: 'upload',
            span: 24,
            // display: true,
            // action: 'api/',
            propsHttp: {
              home: 'http://localhost:3000/'
            },
            disabled: true
          },
          {
            label: '昵称',
            prop: 'username',
            disabled: true
          },
          {
            label: '头像',
            prop: 'avatar',
            dataType: 'string',
            listType: 'picture-img',
            type: 'upload',
            span: 24,
            // display: true,
            // action: 'api/',
            propsHttp: {
              home: 'http://localhost:3000/'
            },
            disabled: true
          },
          {
            label: '创建时间',
            width: 140,
            type: 'datetime',
            prop: 'createdAt',
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            addDisplay: false,
            editDisplay: false,
            viewDisplay: true
          }
        ]
      }
    }
  },
  components: {},
  created() {},
  mounted() {},
  computed: {},
  watch: {},
  methods: {
    downloadFile(url) {
      this.$preView('http://localhost:3000/' + url)
    },
    uploadAfter(res, done) {
      console.log(res, '????????????')
      // this.form.pastPapersDownload = res.data.fileUrl
      done()
    },
    setParams(row) {
      let {
        title,
        examIntroduction,
        registrationTime,
        examSchedule,
        examSyllabus,
        medicalExamQa,
        pastPapersDownload
      } = row
      const regTime = JSON.stringify(registrationTime)
      const examTime = JSON.stringify(examSchedule)
      // const files = JSON.stringify(pastPapersDownload)
      const params = {
        title,
        examIntroduction,
        registrationTime: regTime,
        examSchedule: examTime,
        examSyllabus,
        medicalExamQa,
        pastPapersDownload
      }
      return params
    },
    rowSave(row, done, loading) {
      const params = this.setParams(row)
      console.log(params)
      addCer(params).then(
        () => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        (error) => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowDel(row) {
      this.$confirm('确定将选择编号删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeTopic(row.id)
        })
        .then(() => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    },
    onLoad(page, params = {}) {
      this.loading = true
      getTopicList(page.currentPage, page.pageSize, {
        ...params
      }).then((res) => {
        const data = res.data.data
        this.page.total = res.data.total
        this.data = data
        console.log(res, this.data, res.data, '...............')
        this.loading = false
      })
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage
      // this.onLoad(this.page)
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    refreshChange() {
      this.onLoad(this.page, this.query)
    },
    searchReset() {
      this.query = {}
      this.onLoad(this.page)
    },
    searchChange(params, done) {
      this.query = params
      this.page.currentPage = 1
      this.onLoad(this.page, this.query)
      done()
    }
  }
}
</script>
